package scut.facerecognition.controller;

import cn.dev33.satoken.stp.StpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import scut.facerecognition.entity.dto.CommentDTO;
import scut.facerecognition.entity.dto.PageResult;
import scut.facerecognition.entity.pojo.PostComment;
import scut.facerecognition.result.Result;
import scut.facerecognition.service.CommentService;

import java.util.HashMap;
import java.util.Map;

/**
 * 评论相关接口
 */
@RestController
@RequestMapping("/api")
public class CommentController {
    
    @Autowired
    private CommentService commentService;
    
    /**
     * 获取帖子评论列表接口
     */
    @GetMapping("/posts/{id}/comments")
    public Result getCommentList(
            @PathVariable Long id,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int size) {
        
        PageResult<CommentDTO> pageResult = commentService.getCommentList(id, page, size);
        
        Map<String, Object> data = new HashMap<>();
        data.put("comments", pageResult.getRecords());
        data.put("total", pageResult.getTotal());
        data.put("pages", pageResult.getPages());
        
        return Result.success(data);
    }
    
    /**
     * 发表评论接口
     */
    @PostMapping("/comments")
    public Result addComment(@RequestBody PostComment comment) {
        // 验证用户是否登录
        StpUtil.checkLogin();
        
        // 设置用户ID
        comment.setUserId(StpUtil.getLoginIdAsLong());
        
        // 创建评论
        Long commentId = commentService.addComment(comment);
        
        Map<String, Object> data = new HashMap<>();
        data.put("commentId", commentId);
        
        return Result.success(data);
    }
}
